package com.bjpowernode.springboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjpowernode.springboot.entity.User;
import com.bjpowernode.springboot.mapper.UserMapper;
import com.bjpowernode.springboot.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yan
 * @since 2021-10-12
 */
@RestController
@RequestMapping("/userPlus")
@Api(value = "/userPlus", description = "用户操作接口")
public class UserController {
    @Autowired
    IUserService iUserService;
    @Autowired
    UserMapper userMapper;
    @ApiOperation(value = "根据ID查询")
    @GetMapping("/get")
    public User getTestById(@RequestParam int id){
        return iUserService.getById(id);
    }

    @ApiOperation(value = "查询")
    @GetMapping("/sel")
    public List<User> testSeervice(){
        List<User> list = iUserService.list();
        System.out.println(list);
        return list;
    }

    @ApiOperation(value = "插入")
    @GetMapping("/save")
    public boolean testInsert(User user){
        return iUserService.save(user);
    }


    @ApiOperation(value = "批量删除")
    @PostMapping("/remove")
    public boolean testDelete(@RequestBody List<Integer> ids){
        return iUserService.removeByIds(ids );

    }

    @ApiOperation(value = "更新")
    @PostMapping("/update")
    public boolean testUpdate(User user){
        return iUserService.updateById(user);
    }

    @ApiOperation(value = "查询idname")
    @GetMapping("/userID")
    public List<User> testSelect01() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//        queryWrapper.select(User.class, new Predicate<TableFieldInfo>() {
//            @Override
//            public boolean test(TableFieldInfo tableFieldInfo) {
//                return "name".equals(tableFieldInfo.getColumn());
//            }
//        });
        queryWrapper.select(User.class, tableFieldInfo -> "name".equals(tableFieldInfo.getColumn()));
        List<User> userList = userMapper.selectList(queryWrapper);
        System.out.println(userList);
        return userList;
    }

    @ApiOperation(value = "查询idname")
    @GetMapping("/userByID")
    public List<User> testSelect02(@RequestParam Integer id) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId,id);
        List<User> userList = userMapper.selectList(queryWrapper);
        return userList;
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/userPage")
    public IPage<User> testPage(@RequestParam Integer size,@RequestParam Integer current){
        IPage<User> page = new Page<>();
        //设置每页条数
        page.setSize(size);
        //设置查询第几页
        page.setCurrent(current);
        IPage<User> userIPage = userMapper.selectPage(page, null);
        return userIPage;
    }


}
